package com.jhgsys.internal.accident.controller;

import com.jhgsys.internal.accident.dto.AccidentSurveyTeamDto;
import com.jhgsys.internal.accident.entity.AccidentSurveyTeam;
import com.jhgsys.internal.accident.service.IAccidentSurveyTeamService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 *  Controller
 *
 * @author zhangwei
 * @date 2024-09-11 15:21:32
 */
@Slf4j
@Validated
@Controller
public class AccidentSurveyTeamController extends BaseController {

    @Autowired
    private IAccidentSurveyTeamService accidentSurveyTeamService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentSurveyTeam")
    @RequiresPermissions("accidentSurveyTeam:view")
    public String accidentSurveyTeamIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("accidentSurveyTeam/accidentSurveyTeamList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentSurveyTeam/add")
    @RequiresPermissions("accidentSurveyTeam:add")
    public String accidentSurveyTeamAdd(){
        return FebsUtil.view("accidentSurveyTeam/accidentSurveyTeamEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentSurveyTeam/update/{id}")
    @RequiresPermissions("accidentSurveyTeam:edit")
    public String accidentSurveyTeamUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("accidentSurveyTeam", accidentSurveyTeamService.findAccidentSurveyTeamById(id));
        return FebsUtil.view("accidentSurveyTeam/accidentSurveyTeamEdit");
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-09-11 15:21:32
     */
    @GetMapping("accidentSurveyTeam")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeam:view")
    public FebsResponse getAllAccidentSurveyTeams(AccidentSurveyTeam accidentSurveyTeam) {
        return new FebsResponse().success().data(accidentSurveyTeamService.findAccidentSurveyTeamList(accidentSurveyTeam));
    }

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-09-11 15:21:32
     */
    @GetMapping("accidentSurveyTeam/list")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeam:view")
    public FebsResponse accidentSurveyTeamList(QueryRequest request, AccidentSurveyTeamDto accidentSurveyTeamDto) {
        Map<String, Object> dataTable = getDataTable(this.accidentSurveyTeamService.findAccidentSurveyTeamPage(request, accidentSurveyTeamDto));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 AccidentSurveyTeam
     * @author zhangwei
     * @date 2024-09-11 15:21:32
     */
    @Log("新增AccidentSurveyTeam")
    @PostMapping("accidentSurveyTeam")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeam:add")
    public FebsResponse addAccidentSurveyTeam(@RequestBody AccidentSurveyTeamDto accidentSurveyTeamDto) throws FebsException {
        //必填校验
        checkNecessaryColumns(accidentSurveyTeamDto,false);
        try {
            accidentSurveyTeamDto.setCreatedBy(getCurrentUser().getUserName());
            this.accidentSurveyTeamService.createAccidentSurveyTeam(accidentSurveyTeamDto);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增AccidentSurveyTeam失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 AccidentSurveyTeam
     * @author zhangwei
     * @date 2024-09-11 15:21:32
     */
    @Log("逻辑删除AccidentSurveyTeam")
    @GetMapping("accidentSurveyTeam/delete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeam:delete")
    public FebsResponse deleteAccidentSurveyTeam(@PathVariable("id") String accidentSurveyTeamIds) throws FebsException {
        try {
            AccidentSurveyTeam accidentSurveyTeam = new AccidentSurveyTeam();
            accidentSurveyTeam.setDeletedBy(getCurrentUser().getUserName());
            accidentSurveyTeam.setDeletedTime(new Date());
            accidentSurveyTeam.setDeletedFlg(FebsConstant.INVALID);
            this.accidentSurveyTeamService.deleteAccidentSurveyTeam(accidentSurveyTeam,accidentSurveyTeamIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentSurveyTeam失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 AccidentSurveyTeam
    * @author zhangwei
    * @date 2024-09-11 15:21:32
    */
    @Log("物理删除AccidentSurveyTeam")
    @GetMapping("accidentSurveyTeam/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeam:delete")
    public FebsResponse physicallyDeleteAccidentSurveyTeam(@PathVariable("id") String accidentSurveyTeamIds) throws FebsException {
        try {
            this.accidentSurveyTeamService.physicallyDeleteAccidentSurveyTeam(accidentSurveyTeamIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentSurveyTeam失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 AccidentSurveyTeam
     * @author zhangwei
     * @date 2024-09-11 15:21:32
     */
    @Log("修改AccidentSurveyTeam")
    @PostMapping("accidentSurveyTeam/update")
    @ResponseBody
    @RequiresPermissions("accidentSurveyTeam:edit")
    public FebsResponse updateAccidentSurveyTeam(@RequestBody AccidentSurveyTeamDto accidentSurveyTeamDto) throws FebsException {
        //必填校验
        checkNecessaryColumns(accidentSurveyTeamDto,true);
        try {
            accidentSurveyTeamDto.setUpdatedBy(getCurrentUser().getUserName());
            this.accidentSurveyTeamService.updateAccidentSurveyTeam(accidentSurveyTeamDto);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改AccidentSurveyTeam失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 新增/编辑保存时，字段必填项校验
     * @author zhangwei
     * @date 2024-09-11 15:21:32
     */
    public void checkNecessaryColumns(AccidentSurveyTeamDto accidentSurveyTeamDto,boolean editFlg) throws FebsException {
        if(editFlg && StringUtils.isEmpty(accidentSurveyTeamDto.getSurveyTeamId())){
            throw new FebsException("主键不能为空");
        } else if(StringUtils.isEmpty(accidentSurveyTeamDto.getDutyName())){
            throw new FebsException("智能名称不能为空");
        } else if(null ==accidentSurveyTeamDto.getSort()){
            throw new FebsException("小组排序不能为空");
        } else if(CollectionUtils.isEmpty(accidentSurveyTeamDto.getTeamUserList())){
            throw new FebsException("请选择小组成员");
        }
    }

}
